/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.message.service.impl;

import cn.icanci.message.service.JavaMailByQqService;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

/**
 * @author icanci
 * @date Created in 2021/04/28 11:02
 * @version 1.0
 */
@Slf4j
@Service
public class JavaMailByQqServiceImpl implements JavaMailByQqService {
    @Resource
    private JavaMailSenderImpl mailSender;

    /**
     * 测试邮件发送
     *
     * @param message
     * @param targetEmailAddress
     */
    @Override
    public void sendMail(String message, String targetEmailAddress) {
        // 一个简单的邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 标题
        simpleMailMessage.setSubject("通知：少年你好");
        // 内容
        simpleMailMessage.setText(message);
        // 发给谁
        simpleMailMessage.setTo(targetEmailAddress);
        // 谁发的
        simpleMailMessage.setFrom("1845666903@qq.com");
        // 发送
        mailSender.send(simpleMailMessage);
    }

    @Override
    public void sendMail(String subject, String text, String to, String from) {
        // 一个简单的邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 标题
        simpleMailMessage.setSubject(subject);
        // 内容
        simpleMailMessage.setText(text);
        // 发给谁
        simpleMailMessage.setTo(to);
        // 谁发的
        simpleMailMessage.setFrom(from);
        // 发送
        mailSender.send(simpleMailMessage);
    }

    @Override
    public void sendMoreMail(String subject, String text, String from, String to) throws javax.mail.MessagingException {
        // 一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        // 标题
        helper.setSubject(subject);
        // 发送的内容
        // 设置内容，是否为html数据
        helper.setText(text, true);
        // 谁发的
        helper.setFrom(from);
        // 发给谁
        helper.setTo(to);
        mailSender.send(mimeMessage);
    }

    @Override
    public void sendMoreMail(String subject, String text, String from, String to, Map<String, File> map) throws javax.mail.MessagingException {
        // 一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        // 标题
        helper.setSubject(subject);
        // 发送的内容
        // 设置内容，是否为html数据
        helper.setText(text, true);
        // 附件
        if (null != map && !map.isEmpty()) {
            Set<Map.Entry<String, File>> entries = map.entrySet();
            Iterator<Map.Entry<String, File>> iterator = entries.iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, File> next = iterator.next();
                helper.addAttachment(next.getKey(), next.getValue());
            }
        }
        // 谁发的
        helper.setFrom(from);
        // 发给谁
        helper.setTo(to);
        mailSender.send(mimeMessage);
    }
}
